package com.zlx.taskcenter.common;


import com.zlx.taskcenter.consumer.BaseConsumer;

/**
 * 队列配置
 */
public class RedisQueueConfiguration {

    /**
     * 队列名称
     */
    private String queue;
    /**
     * 消费者数量
     */
    private Integer consumerNum;
    /**
     * 消费者
     */
    private BaseConsumer consumer;

    public Integer getConsumerNum() {
        return consumerNum;
    }

    String getQueue() {
        return queue;
    }

    BaseConsumer getConsumer() {
        return consumer;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {

        private RedisQueueConfiguration configuration = new RedisQueueConfiguration();

        public RedisQueueConfiguration defaultConfiguration(BaseConsumer consumer) {
            configuration.consumer = consumer;
            configuration.queue = consumer.getClass().getSimpleName();
            return configuration;
        }

        public Builder queue(String queue) {
            configuration.queue = queue;
            return this;
        }

        public Builder consumerNum(Integer consumerNum) {
            configuration.consumerNum = consumerNum;
            return this;
        }

        public <T extends BaseConsumer> Builder consumer(T consumer) {
            configuration.consumer = consumer;
            return this;
        }

        public RedisQueueConfiguration build() {
            if (configuration.queue == null || configuration.queue.length() == 0) {
                if (configuration.consumer != null) {
                    configuration.queue = configuration.getClass().getSimpleName();
                }
            }
            return configuration;
        }

    }
}
